My team is developing a login/register DB system. According to some guides, it's usually used a combination of props (to set states) and a fetch with POST.
login.js
export default class App extends Component {
state = {
usuario: '',
setUsuario: '',
senha: '',
setSenha: ''
}
async sendForm() {
let response = await fetch('http://{My NAT IP}:3000/login', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
usuario: '',
senha: ''
})
})};
render() {
return(
<View style = { styles.container } >
<Text style={styles.titulo}>
BOM DENTE
</Text>
<Image
style={styles.logo}
source={require('../assets/icon.png')}
/>
<TextInput
style={styles.input}
placeholder="Nome de usuário"
onChangeText={text => this.state.setUsuario}
/>
<TextInput
style={styles.input}
secureTextEntry={true}
placeholder="Senha"
onChangeText={text => this.state.setSenha}
/>
<TouchableOpacity
style={styles.botao}
onPress={() => this.sendForm()}
>
<Text style={styles.botaoText}>
LOGIN
</Text>
</TouchableOpacity>
</View >
)
}
}
Controller.js
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const models = require('./models');
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
let user = models.Usuario;
app.post('/login', (req, res) => {
console.log(req.body)
})
let port = process.env.PORT || 3000;
app.listen(port, (req, res) => {
console.log("Servidor operacional");
})
P.S.: According to this video, expo requires the NAT IP in the fetch instead of localhost.